package com.leyou.sms.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.leyou.common.utils.JsonUtils;
import com.leyou.sms.config.SmsConstants;
import com.leyou.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 短信工具类
 * aliyun短信服务的java实例代码:
 *
 * pom.xml
 * <dependency>
 *   <groupId>com.aliyun</groupId>
 *   <artifactId>aliyun-java-sdk-core</artifactId>
 *   <version>4.5.3</version>
 * </dependency>
 *
 * ...
 *
 * request.setSysMethod(MethodType.POST);
 * request.setSysDomain("dysmsapi.aliyuncs.com");
 * request.setSysVersion("2017-05-25");
 * request.setSysAction("SendSms");
 *
 * ...
 *
 * 我目前的pom文件依赖的是
 * <groupId>com.aliyun</groupId>
 *      <artifactId>aliyun-java-sdk-core</artifactId>
 *      <version>4.1.1</version>
 *  所以应该改为:
 *  request.setMethod(MethodType.POST);
 *  request.setDomain(smsProps.getDomain());
 *  request.setVersion(smsProps.getVersion());
 *  request.setAction(smsProps.getAction());
 *
 */
@Component
@Slf4j
public class SmsHelper {
    @Autowired
    private IAcsClient client;
    @Autowired
    private SmsProperties smsProps;

    /**
     * 发送短信
     */
    public void sendSms(String phone,String code){
        CommonRequest request = new CommonRequest();
        //不用具体的值，让这个发送短信的方法具有灵活性
        request.setMethod(MethodType.POST);
        request.setDomain(smsProps.getDomain());
        request.setVersion(smsProps.getVersion());
        request.setAction(smsProps.getAction());
        request.putQueryParameter(SmsConstants.SMS_PARAM_REGION_ID, smsProps.getRegionID());
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_PHONE, phone);
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_SIGN_NAME, smsProps.getSignName());
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_TEMPLATE_CODE, smsProps.getVerifyCodeTemplate());
        //String smsCode="{\"name\":\"Tom\", \"code\":\""+code+"\"}";
        request.putQueryParameter(SmsConstants.SMS_PARAM_KEY_TEMPLATE_PARAM, "{\""+smsProps.getCode()+"\":\""+code+"\"}");
        try {
            //调用阿里短信API发送短信
            CommonResponse response = client.getCommonResponse(request);

            //取出message和code，判断是否为OK
            String result = response.getData();
            /**
             * getData()获得的数据：
             * {
             * 	"RequestId": "B7FC7AE8-5D78-48FB-9686-5EEC1F2ACF2C",
             * 	"Message": "OK",
             * 	"BizId": "941706808718760628^0",
             * 	"Code": "OK"
             * }
             * 我们需要获取它的Message和Code判断是否为OK，而且是同时OK
             */
            //转换Map对象，因为键和值都是String，所以JsonUtils.toMap(result, String.class, String.class)
            Map<String, String> resultMap = JsonUtils.toMap(result, String.class, String.class);

            if(resultMap.get(SmsConstants.SMS_RESPONSE_KEY_MESSAGE).equals(SmsConstants.OK)
                    && resultMap.get(SmsConstants.SMS_RESPONSE_KEY_CODE).equals(SmsConstants.OK)){
                log.info("【短信业务】发送短信成功");
            }else{
                log.error("【短信业务】发送短信失败，原因："+resultMap.get(SmsConstants.SMS_RESPONSE_KEY_MESSAGE));
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("【短信业务】发送短信失败，原因："+e.getMessage());
        }
    }

}
